<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Class Order 订单管理
 * @author yw
 * 2017年6月6日16:24:27
 */

class Order extends Web_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->userSession();
        $this->load->model('Order_model','order_model');
        $this->load->model('Orderdetail_model');
        $this->load->model('Zone_model');
        $this->load->helper('page_helper');
    }

    /**
     * [orderList 订单列表 统计]
     * @return [array] [订单详情列表] 
     */
    public function orderList()
    {
        $page = $this->input->get('per_page');//数据偏移量
        $limit = 10;//每页记录数
        if(!ctype_digit($page)) 
        {
            $page = 0;
        }
        $start_time = strtotime($this->input->get('start_time'));//开始时间
        $end_time = strtotime($this->input->get('end_time'));//结束时间
        $orderid = $this->input->get('name');//订单id
        $status = $this->input->get('status');//1=支付成功,2=已发货,3=未支付,4=超时,5=已退押金
        $order = $this->order_model->getOrderByQuery($start_time,$end_time,$status,$orderid,isset($_GET['export']) ? null : $limit,isset($_GET['export']) ? null : $page);
        $count = $this->order_model->countOrder($start_time,$end_time,$status,$orderid);
        if(empty($order))
        {
            $result['data'] = array();
        }
        else
        {
            //获取详细的地理位置
            foreach ($order as $k_3 => $v_3) {
                $p = json_decode($v_3['address'],true);
                $res = $this->Zone_model->getZonesStringById($p['district']);
                $order[$k_3]['consignee'] = $p['consignee'];
                $order[$k_3]['address'] = $res.$p['address'];
                $order[$k_3]['mobile'] = $p['mobile'];
                switch ($v_3['status']) {
                    case '1':
                        $order[$k_3]['status'] = '支付成功';
                        break;
                    case '2':
                        $order[$k_3]['status'] = '已发货';
                        break;
                    case '3':
                        $order[$k_3]['status'] = '未支付';
                        break;
                    case '4':
                        $order[$k_3]['status'] = '超时';
                        break;
                    case '5':
                        $order[$k_3]['status'] = '已退押金';
                        break;
                }
            }
        }
        if(isset($_GET['export']))
        {
            $res = array();
            foreach ($order as $k => $v) {
                $r[0] = $v['order_id'];
                $r[1] = $v['price'];
                $r[2] = $v['status'];
                $r[3] = $v['paytype']==1 || $v['paytype']==2 ? '微信':'支付宝';
                $r[4] = $v['amount'];
                $r[5] = date('Y-m-d H:i:s',$v['create_time']);
                $r[6] = $v['consignee'];
                $r[7] = $v['address'];
                $r[8] = $v['mobile'];
                $r[9] = $v['leave'];
                $r[10] = $v['express'];
                $r[11] = chunk_split($v['logistics'],20," ");
                $res[] = $r;
                unset($v[$k]);
            }
            $header = array('订单id','总价格','状态','支付方式','数量','创建时间','买家姓名','详细地址','手机号码','买家备注','物流公司名','物流单号');
            $this->downOrder($header,$res);
        }
        $arr['data'] = $order;
        $arr['page'] = getPage($this->config->item('base_url').'admin/order/orderlist', $count, $limit);
        unset($order);
        $this->load->view('admin/header.php');
        $this->load->view('admin/orderdetailed',$arr);
    }
    /**
     * [orderdetail 根据订单id查询订单详情]
     * @return [type] [description]
     */
    public function orderdetail()
    {   
        $order_id = $this->input->get('order_id');
        $orderlsit = $this->order_model->getOrderById($order_id);
        if(empty($orderlsit))
        {
            header('Location:'.$this->base_url.'admin/order/orderlist');die;
        }
        $json = json_decode($orderlsit['address']);
        $orderlsit['consignee'] = $json->consignee;
        switch ($orderlsit['status']) {
            case '1':
                $orderlsit['status'] = '支付成功';
                break;
                case '2':
                $orderlsit['status'] = '已发货';
                break;
                case '3':
                $orderlsit['status'] = '未支付';
                break;
                case '4':
                $orderlsit['status'] = '超时';
                break;
                case '5':
                $orderlsit['status'] = '已退押金';
                break;
        }
        $orderdetail = $this->Orderdetail_model->getOrderByOrderid($order_id);
        $arr['data'] = $orderlsit;
        $arr['orderdetail'] = $orderdetail;
        $this->load->view('admin/header');
        $this->load->view('admin/orderdetail',$arr);
    }
    //下载网页统计
    public function downOrder($header,$arr)
    {
        $this->load->library('PHPExcel');
        $this->load->helper('excel');
        excel_download($header,$arr);
    }

    /**
     * [shipments 待发货数据导出]
     */
    public function shipments()
    {
        $arr['data'] = $this->order_model->getOrderToShipment();
        if($this->input->get('export'))
        {
            $res =array();
            foreach ($arr['data'] as $k => $v) {
                $res[$k][0] = $v['order_id'];
                $res[$k][1] = $v['username'];
                $res[$k][2] = $v['mobile'];
                $res[$k][3] = $v['address'];
                $res[$k][4] = $v['det'];
            }
            $header = array('订单号','姓名','电话','详细地址','订单明细');
            $this->downOrder($header,$res);
        }
        $this->load->view('admin/header');
        $this->load->view('admin/shipments',$arr);
    }
}